﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;
using Jy123.Web.AppCode;

namespace Jy123.Web.Mood
{
    public partial class AllMood : System.Web.UI.Page
    {
        private int userID = 1;//暂时写死用户ID
        List<int?> count;
        protected void Page_Load(object sender, EventArgs e)
        {
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                Response.Redirect("/");
            }
            userID = int.Parse(strUserIdFromCookie);
            StringBuilder sbHtml = new StringBuilder();
            sbHtml = CommonHtml(sbHtml, "user_moodmain");//公共部分的html
            sbHtml.Replace("$whoseone$", "所有");//Header标题名称
            sbHtml.Replace("$FaceList$", new Jy123.BLL.User.UserSql().FaceList(Cache["user_face"].ToString()));//初始化表情列表
            //连接地址
            sbHtml.Replace("$FriendClassSel$", "")
                .Replace("$MYClassSel$", "")
                .Replace("$AllClassSel$", "sel");
            sbHtml.Replace("$MoodList$", MoodList(userID, out count));//心情列表
            sbHtml.Replace("$MoodCount$", count[0].ToString());//统计心情数量
            Response.Write(sbHtml.ToString());
        }
        /// <summary>
        /// 公共部分的html
        /// </summary>
        /// <param name="sbHtml">最终写出页面的大StringBuilder</param>
        /// <param name="cacheHtml">缓存名称</param>
        /// <returns>StringBuilder</returns>
        private StringBuilder CommonHtml(StringBuilder sbHtml, string cacheHtml)
        {
            sbHtml = new StringBuilder(Cache[cacheHtml].ToString());
            sbHtml = sbHtml.Replace("$Header$", Cache["user_header"].ToString());//头部
            sbHtml = sbHtml.Replace("$Footer$", Cache["user_footer"].ToString());//底部
            sbHtml = sbHtml.Replace("$LeftUserCmd$", Cache["user_navigation_up"].ToString());//左侧导航
            sbHtml = sbHtml.Replace("$LeftUserFriendAdd$", Cache["user_friendadd"].ToString());//左侧直接添加好友
            return sbHtml;
        }

        /// <summary>
        /// 心情列表
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <param name="cpage">起始页</param>
        /// <param name="psize">页记录数</param>
        /// <returns>字符串</returns>
        private string MoodList(int userID,  out List<int?> count)
        {
            int currentpage = 1;//当前页
            int pagesize = 4;//页记录大小

            //把被查询页赋值给当前页
            if (!string.IsNullOrEmpty(Request.QueryString["page"]))
            {
                currentpage = Convert.ToInt32(Request.QueryString["page"]);
            }

            string strHtml = Cache["user_moodlist"].ToString();
            StringBuilder sbHtml = new StringBuilder();
            //List<int?> count;
            DataTable dtMoodList = new Jy123.BLL.User.Mood().MoodList_NewAll(userID, currentpage, pagesize, out count).Tables[0];
            for (int i = 0; i < dtMoodList.Rows.Count; i++)
            {
                string str = strHtml;
                str = str.Replace("$MoodID$", dtMoodList.Rows[i]["ID"].ToString());
                str = str.Replace("$UserID$", dtMoodList.Rows[i]["FUser"].ToString());
                str = str.Replace("$UserName$", dtMoodList.Rows[i]["UserName"].ToString());
                str = str.Replace("$Portrait$", dtMoodList.Rows[i]["Portrait"].ToString());
                str = str.Replace("$PostTime$", dtMoodList.Rows[i]["PostTime"].ToString());
                str = str.Replace("删除", string.Empty);
                if (!string.IsNullOrEmpty(dtMoodList.Rows[i]["MType"].ToString()))
                {
                    str = str.Replace("$MoodContent$", "[" + dtMoodList.Rows[i]["MType"].ToString() + "]" + Jy123.BLL.User.UserSql.BlogContent(dtMoodList.Rows[i]["MoodContent"].ToString()));
                }
                else
                {
                    str = str.Replace("$MoodContent$", Jy123.BLL.User.UserSql.BlogContent(dtMoodList.Rows[i]["MoodContent"].ToString()));
                }
                str = str.Replace("$MoodComments$", MoodComments(Convert.ToInt32(dtMoodList.Rows[i]["ID"]),out count));
                str = str.Replace("$CommentCount$", count[0].ToString());
                sbHtml.Append(str);
            }
            string pageHtml = @"
                        <ul>
                            <li class="">
                                <span>
                                       {0}
                                </span>
                                <b>第{1}-{2}个/共{3}个</b>
                            </li>
                        </ul>";
            string strHref = "AllMood.aspx?page=$num$";
            MoonPage mp = new MoonPage(false, true);
            mp.Count = Convert.ToInt32(count[1]);
            sbHtml.AppendFormat(pageHtml, mp.GetStrPage(currentpage, "no", "sel", strHref), 1 + (currentpage - 1) * pagesize, ((currentpage * pagesize) > count[0]) ? count[0] : currentpage * pagesize, count[0]);
            
            return sbHtml.ToString();
        }

        /// <summary>
        /// 评论列表
        /// </summary>
        /// <param name="moodID">心情ID</param>
        /// <returns>字符串</returns>
        private string MoodComments(int moodID, out List<int?> count)
        {
            //List<int?> count;
            StringBuilder sbHtml = new StringBuilder();
            string strHtml = Cache["user_moodcomment"].ToString();
            DataTable dtMoodCommList = new Jy123.BLL.User.Mood().MoodCommentListByMoodID(moodID, 1, int.MaxValue, out count).Tables[0];
            //string strDel = "<a href=\"javascript:myAjax('/Mood/MoodCommentDel.ashx','CommentID='+$CommentID$);\" class=\"imgdel\" ><img src=\"/images/del_blog.gif\" alt=\"删除\" /></a>";
            string strDel = "<img src=\"/images/del_blog.gif\" alt=\"删除\" onclick=\"javascript:myAjax('/Mood/MoodCommentDel.ashx','CommentID='+$CommentID$);\" />";
            for (int i = 0; i < dtMoodCommList.Rows.Count; i++)
            {
                string str = strHtml;
                if (dtMoodCommList.Rows[i]["FUserCommentator"].ToString().Equals(userID.ToString()))
                {
                    str = str.Replace("$imgdel$", strDel);
                }
                else
                {
                    str = str.Replace("$imgdel$", string.Empty);
                }

                str = str.Replace("$MoodID$", dtMoodCommList.Rows[i]["FMood"].ToString());
                str = str.Replace("$CommentID$", dtMoodCommList.Rows[i]["ID"].ToString());
                str = str.Replace("$UserID$", dtMoodCommList.Rows[i]["FUserCommentator"].ToString());
                str = str.Replace("$UserName$", dtMoodCommList.Rows[i]["UserName"].ToString());
                str = str.Replace("$CommentContent$", dtMoodCommList.Rows[i]["CommentContent"].ToString());
                str = str.Replace("$PostTime$", dtMoodCommList.Rows[i]["PostTime"].ToString());
                sbHtml.Append(str);
            }
            return sbHtml.ToString();
        }
    }
}
